<?php

namespace app\model;

use think\Model;

class Role extends Model
{
    public function insertData(): array
    {
        $param = request()->post();
        if (!empty($param['r_value'])) $param['r_value'] = json_encode($param['r_value'], 320);
        $this->exists(!empty($param['id']))->save($param);
        return $this->toArray();
    }

    public function lst(): array
    {
        $param = request()->param();
        $data = $this->where('is_del', 1)->paginate(['list_rows' => 30, 'page' => $param['page'] ?? 1])->toArray();
        if (!empty($data['data'])) {
            $data['data'] = array_map(function ($item) {
                $item['r_value'] = json_decode($item['r_value'], true);
                return $item;
            }, $data['data']);
        }
        return pageData($data);
    }

    public function deatil()
    {
        $param = request()->param("id");
        $data = $this->where('id', $param)->where('is_del', 1)->find();
        if(empty($data)) throw new \Exception("权限不存在！");
        $data = $data->toArray();
        $data['r_value'] = json_decode($data['r_value'], true);
        return $data;
    }
}
